package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.mapper.BusinessMapper;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @Autowired
    private BusinessMapper businessMapper;
    /**
     *1.商机列表查询
     */
    @GetMapping
    public Result businessList(BusinessDto businessDto) {
      PageResult<Business> pageResult = businessService.listbusiness(businessDto);
      return Result.success(pageResult);
    }
    /**
     * 2.添加商机
     */
    @PostMapping
    public Result add(Business business) {
        business.setStatus(1);
        business.setUpdateTime(LocalDateTime.now());
        business.setCreateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }
    /**
     * 3.分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("商机分配:{} ,{}", businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    /**
     * 4.踢回公海
     */
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        log.info("踢回公海: {}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    /**
     * 5.转客户
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户: {}", id);
      businessService.toCustomer(id);
        return Result.success();
    }



    /**
     * 6.根据id查询商机(跟进商机一)
     */
    @GetMapping("/{id}")
    public  Result getBusinessById(@PathVariable Integer id){
        log.info("查询商机: {}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    /**
     * 7.跟进商机(跟进商机二)
     */
    @PutMapping
    public Result updateBusiness(@RequestBody Business business) {
        log.info("更新商机: {}", business);
        business.setUpdateTime(LocalDateTime.now());
        businessService.trackBusiness(business);
        return Result.success();
    }
    /**
     * 8.公海池列表
     */
    @GetMapping("/pool")
    public Result poolList(BusinessDto businessDto) {
        log.info("查询公海列表: {}", businessDto);
        PageResult<Business> pageResult = businessService.poolList(businessDto);
        return Result.success(pageResult);
    }
}
